﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DeviceInfoManage.DataAccessLayer;

namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    ///DailyInspect
    /// </summary>
    [Serializable]
    public class DailyInspect
    {
        #region 字段属性
        private int _id;
        private string _itemnumber;
        private string _department;
        private string _principal;
        private DateTime _date;
        private string _errtype;
        private string _state;
        private string _issolve;
        private string _errreason;
        private string _errsolve;
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 巡检项目号
        /// </summary>
        public string ItemNumber
        {
            set { _itemnumber = value; }
            get { return _itemnumber; }
        }
        /// <summary>
        /// 巡检部门
        /// </summary>
        public string Department
        {
            set { _department = value; }
            get { return _department; }
        }
        /// <summary>
        /// 负责人
        /// </summary>
        public string Principal
        {
            set { _principal = value; }
            get { return _principal; }
        }
        /// <summary>
        /// 巡检日期
        /// </summary>
        public DateTime Date
        {
            set { _date = value; }
            get { return _date; }
        }
        /// <summary>
        /// 错误类型
        /// </summary>
        public string ErrType
        {
            set { _errtype = value; }
            get { return _errtype; }
        }
        /// <summary>
        /// 巡检类别
        /// </summary>
        public string State
        {
            set { _state = value; }
            get { return _state; }
        }
        /// <summary>
        /// 故障是否解决
        /// </summary>
        public string IsSolve
        {
            set { _issolve = value; }
            get { return _issolve; }
        }
        /// <summary>
        /// 错误原因
        /// </summary>
        public string ErrReason
        {
            set { _errreason = value; }
            get { return _errreason; }
        }
        /// <summary>
        /// 解决办法
        /// </summary>
        public string ErrSolve
        {
            set { _errsolve = value; }
            get { return _errsolve; }
        }
        #endregion

        #region 构造函数
        public DailyInspect()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "DailyInspect");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DailyInspect");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DailyInspect model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DailyInspect(");
            strSql.Append("ItemNumber,Department,Principal,Date,ErrType,State,IsSolve,ErrReason,ErrSolve)");
            strSql.Append(" values (");
            strSql.Append("@ItemNumber,@Department,@Principal,@Date,@ErrType,@State,@IsSolve,@ErrReason,@ErrSolve)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNumber", SqlDbType.VarChar,200),
					new SqlParameter("@Department", SqlDbType.VarChar,50),
					new SqlParameter("@Principal", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@ErrType", SqlDbType.VarChar,50),
					new SqlParameter("@State", SqlDbType.Char,1),
					new SqlParameter("@IsSolve", SqlDbType.Char,1),
					new SqlParameter("@ErrReason", SqlDbType.VarChar,500),
					new SqlParameter("@ErrSolve", SqlDbType.VarChar,500)};
            parameters[0].Value = model.ItemNumber;
            parameters[1].Value = model.Department;
            parameters[2].Value = model.Principal;
            parameters[3].Value = model.Date;
            parameters[4].Value = model.ErrType;
            parameters[5].Value = model.State;
            parameters[6].Value = model.IsSolve;
            parameters[7].Value = model.ErrReason;
            parameters[8].Value = model.ErrSolve;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(DailyInspect model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DailyInspect set ");
            strSql.Append("ItemNumber=@ItemNumber,");
            strSql.Append("Department=@Department,");
            strSql.Append("Principal=@Principal,");
            strSql.Append("Date=@Date,");
            strSql.Append("ErrType=@ErrType,");
            strSql.Append("State=@State,");
            strSql.Append("IsSolve=@IsSolve,");
            strSql.Append("ErrReason=@ErrReason,");
            strSql.Append("ErrSolve=@ErrSolve");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@ItemNumber", SqlDbType.VarChar,200),
					new SqlParameter("@Department", SqlDbType.VarChar,50),
					new SqlParameter("@Principal", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@ErrType", SqlDbType.VarChar,50),
					new SqlParameter("@State", SqlDbType.Char,1),
					new SqlParameter("@IsSolve", SqlDbType.Char,1),
					new SqlParameter("@ErrReason", SqlDbType.VarChar,500),
					new SqlParameter("@ErrSolve", SqlDbType.VarChar,500)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ItemNumber;
            parameters[2].Value = model.Department;
            parameters[3].Value = model.Principal;
            parameters[4].Value = model.Date;
            parameters[5].Value = model.ErrType;
            parameters[6].Value = model.State;
            parameters[7].Value = model.IsSolve;
            parameters[8].Value = model.ErrReason;
            parameters[9].Value = model.ErrSolve;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyInspect ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DailyInspect GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,ItemNumber,Department,Principal,Date,ErrType,State,IsSolve,ErrReason,ErrSolve from DailyInspect ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DailyInspect model = new DailyInspect();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.ItemNumber = ds.Tables[0].Rows[0]["ItemNumber"].ToString();
                model.Department = ds.Tables[0].Rows[0]["Department"].ToString();
                model.Principal = ds.Tables[0].Rows[0]["Principal"].ToString();
                if (ds.Tables[0].Rows[0]["Date"].ToString() != "")
                {
                    model.Date = DateTime.Parse(ds.Tables[0].Rows[0]["Date"].ToString());
                }
                model.ErrType = ds.Tables[0].Rows[0]["ErrType"].ToString();
                model.State = ds.Tables[0].Rows[0]["State"].ToString();
                model.IsSolve = ds.Tables[0].Rows[0]["IsSolve"].ToString();
                model.ErrReason = ds.Tables[0].Rows[0]["ErrReason"].ToString();
                model.ErrSolve = ds.Tables[0].Rows[0]["ErrSolve"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ItemNumber,Department,Principal,Date,State,IsSolve,ErrReason,ErrSolve ");
            strSql.Append(" FROM DailyInspectView ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,ItemNumber,Department,Principal,Date,ErrType,State,IsSolve,ErrReason,ErrSolve ");
            strSql.Append(" FROM DailyInspect ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}
